package cn.wanda.bi.sunshine.platform.server.service.statistics;

import cn.wanda.bi.sunshine.platform.server.bean.mapper.A06727AppArticleTitleTargetMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppArticleTitleTarget;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppArticleTitleTargetExample;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wangfupeng1
 */
@Service
public class A06727AppArticleTitleTargetService {

    public static final Logger logger = LoggerFactory.getLogger(A06727AppArticleTitleTargetService.class);

    @Autowired
    private A06727AppArticleTitleTargetMapper a06727AppArticleTitleTargetMapper;

    @Cacheable(value="cache:statistics", key = "'cache:statistics:articletitletarget:starttime:'+#startTime+':endtime:'+#endTime+':index:'+#index+':limit:'+#limit")
    public ListResponse<A06727AppArticleTitleTarget> articleTitleTarget(Long startTime, Long endTime, Integer index, Integer limit) {
        A06727AppArticleTitleTargetExample a06727AppArticleTitleTargetExample = new A06727AppArticleTitleTargetExample();
        A06727AppArticleTitleTargetExample.Criteria criteria = a06727AppArticleTitleTargetExample.createCriteria();
        if (startTime != null && StringUtils.isNotEmpty(startTime + ""))
            criteria.andDateKeyGreaterThanOrEqualTo(new Date(startTime));
        if (endTime != null && StringUtils.isNotEmpty(endTime + ""))
            criteria.andDateKeyLessThanOrEqualTo(new Date(endTime));
        int count = a06727AppArticleTitleTargetMapper.countByExample(a06727AppArticleTitleTargetExample);
        a06727AppArticleTitleTargetExample.setOffset(index * limit - limit);
        a06727AppArticleTitleTargetExample.setLimit(limit);
        a06727AppArticleTitleTargetExample.setOrderByClause("date_key desc");
        List<A06727AppArticleTitleTarget> a06727AppArticleTitleTargets = a06727AppArticleTitleTargetMapper.selectByExample(a06727AppArticleTitleTargetExample);
        return ListResponse.success(a06727AppArticleTitleTargets, count, index, limit);
    }

}
